package activities;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import bookless.pakage.R;
import bookless.pakage.UserHandler;

public class NewUserActivity extends Activity{
	
	private static Context context;

	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        
        context = NewUserActivity.this;
		        
        Button submit;
        submit = (Button)findViewById(R.id.registerSubmit);
        submit.setOnClickListener(new View.OnClickListener() {
        
        	@Override
        	public void onClick(View v) {
        		
        		EditText name = (EditText) findViewById(R.id.registerName);
        		String userName = name.getText().toString();
        		
        		EditText p = (EditText) findViewById(R.id.registerPassword);
        		String password = p.getText().toString();
       						
        		String response = UserHandler.addUser(userName, password);
        			
        			//Print out message if and why addUser dint't work, of addUser work send user to login
	        		if(response.equals("ok")){							
	        			startActivity(new Intent("bookless.pakage.LOGIN"));
	        		}else if(response.equals("userExsist")){
	        			Dialog d = new Dialog(NewUserActivity.this);
	        			d.setTitle("Error!");
	        			TextView tv = new TextView(NewUserActivity.this);
	        			tv.setText("This user name already exists.");
	        			d.setContentView(tv);
						d.show();
	        		}else if(response.equals("wrongInData")){
	        			Dialog d = new Dialog(NewUserActivity.this);
	        			d.setTitle("Error!");
	        			TextView tv = new TextView(NewUserActivity.this);
	        			tv.setText("You must fill in user name and password, password must be longen than 3 characters");
	        			d.setContentView(tv);
						d.show();
	        		}
        		
        	}
        });
        
    }

	public static Context getContext() {
		return context;
	}


}